package com.qk.management.controller;

/**
 * @author diva
 * @date 2025/9/29 10:31
 * @ProjectName 部门管理控制层
 */


import  com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping
    public Result page(@RequestParam(name = "name",required = false) String name,
                     @RequestParam(name = "status",required = false) Integer status,
                     @RequestParam(name = "page",defaultValue = "1") Integer page,
                     @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize){

        PageResult<Dept> pageResult = deptService.page(name,status,page,pageSize);
        log.info("查询部门列表，参数：部门名称={}，部门状态={}，当前页码={}，页码大小={}",name,status,page,pageSize);
        log.info("-----------------------++++++部门列表方法+++++--------------------------");

        return Result.success(pageResult);
    }

    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("新增部门，参数：{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getDeptById(@PathVariable("id") Integer id) {
        log.info("查询部门id：{}",id);
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }

    @PutMapping("")
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门：{}",dept);
        deptService.update(dept);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable Integer id) {
        log.info("删除部门id：{}",id);
        deptService.deleteDept(id);
        return Result.success();
    }
    @GetMapping("/list")
    public Result selectAll() {
        log.info("查询部门列表");
        List<Dept> deptList = deptService.selectAll();
        return Result.success(deptList);
    }

}